// pages/debtorBaseInfo/shareholder/shareholder.ts

import { Debtor } from "../../../../../typings/types/debtor";
import { shareholderAPI } from "../../../../api/debtor";
import { ZCC } from "../../../../utils/BasePage";
import { formateDigit, validStr } from "../../../../utils/util";

interface PageData {
  // page: number,
  companyId: string,
  // total: number | undefined,
  // items: Debtor.ShareholderCell[]
}

new ZCC.RefreshListPage<Debtor.ShareholderCell>({

  getShareholderInfo: async function () {

    this.setRefreshing()
    // wx.showLoading()
    try {
      console.log(this.data.page)
      const res = await shareholderAPI(this.data.companyId, this.data.page);
      if (res.content) {
        const list = res.content.list 
        console.log(res.content)
        let items: Debtor.ShareholderCell[] = []
        if (list.length > 0) {
           items = list.map((item) => {
            return {
              name: validStr(item.stockName),
              rows: [
                { title: '股东类型', val: validStr(item.stockType), sectitle: '持股比例', secval: validStr(item.stockPercent.toFixed(2)) + '%' },
                { title: '认缴出资额', val: validStr(formateDigit(item.shouldCapi)), sectitle: '认缴出资时间', secval: validStr(item.shoudDate) },
                { title: '实缴出资额', val: validStr(formateDigit(item.realCapi)), sectitle: '实缴时间', secval: validStr(item.capiDate) },
              ]
            } 
          })
        }
        this.updateDatasWithNewItems(items, res.content.total, res.content.hasNextPage)
        //   items = this.data.items.concat(items)
        //   const page = ++this.data.page
        //   this.setData({
        //     page: page,
        //     items: items,
        //     total: res.content.total || 0
        //   })
        // } else {
        //   this.setData({
        //     total: res.content.total || 0
        //   })
        // }

      }
    } finally {
      this.endRefreshing()
      // wx.stopPullDownRefresh()
      // wx.hideLoading()
    }
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{
    // page: 1,
    companyId: '',
    // total: undefined,
    // items: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      companyId: this.options.companyId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getShareholderInfo()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  
    if (this.resetPageIfNeed()) {
      this.getShareholderInfo()
    }
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getShareholderInfo()
  },
})